<template>
    <div class="goods-info">
        
        <transition 
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter"
        >
            <div class="ball" ref="ball" v-show="ballFlag"></div>
        </transition>

        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <!-- 轮播图区域 -->
                    <mt-swipe :auto="4000">
                        <mt-swipe-item v-for="item in thumbImgs" :key="item.id">
                            <img :src="item.src">
                        </mt-swipe-item>
                    </mt-swipe>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">{{info.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        <span class="old">市场价: <b>￥{{info.market_price}}</b></span>
                        <span class="now">销售价: <b>￥{{info.sell_price}}</b></span>
                    </p>
                    <div class="count">
                        购买数量：
                        <div class="mui-numbox" data-numbox-step='1' data-numbox-min='1'>
                            <button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
                            <input id="test" class="mui-input-numbox" type="number" value="1" />
                            <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
                        </div>
                    </div>
                    <p class="opt">
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addToCar">加入购物车</mt-button>                   
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{info.goods_no}}</p>
                    <p>库存情况：{{info.stock_quantity}}</p>
                    <p>上架时间：{{info.add_time | dateFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>                   
            </div>
        </div>
    </div>
</template>

<script>
import { Toast } from 'mint-ui'
//导入mui.js
import mui from '../../lib/mui/js/mui.min.js'

export default {
    data(){
        return {
            id: this.$route.params.id,//获取路由中的id
            thumbImgs:[],
            info:{},
            ballFlag:false, //控制小球显示隐藏标识符
            selectedCount:1,
        }
    },
    created(){
        this.getThumbImgs()
        this.getGoodsInfo()
    },
    mounted(){
       this.numbox = mui('.mui-numbox').numbox()
    },
    watch:{
        //监听 info 的值，将最终的值动态设置在数字步进器内
        'info':function(newVal, oldVal){
            //console.log(newVal.stock_quantity)
            this.numbox.setOption('step',1)
            this.numbox.setOption('max',newVal.stock_quantity)
        }
    },
    methods:{
        getThumbImgs(){
            this.$http.get('api/getthumimages/' + this.id).then(result => {
                //console.log(result.body)
                if(result.body.status == 0){
                   this.thumbImgs = result.body.message
                }else{
                    Toast('获取图片缩略图失败')
                }
            })
        },
        getGoodsInfo(){
            this.$http.get('api/goods/getinfo/' + this.id).then(result => {
                //console.log(result.body)
                if(result.body.status == 0){
                   this.info = result.body.message[0]
                }else{
                    Toast('获取商品信息失败')
                }
            })
        },
        goDesc(id){
            //使用编程式导航，跳转到 图文介绍 界面
            this.$router.push({name: 'goodsdesc', params: { id }})
        },
        goComment(id){
            //使用编程式导航，跳转到 商品评论 界面
            this.$router.push({name: 'goodscomment', params: { id }})
        },
        addToCar(){
            //添加到购物车
            this.ballFlag = !this.ballFlag;
            let count = this.numbox.getValue()
            //保存到 store 中的商品信息
            let goodsInfo = { id: this.id, count: count, price: this.info.sell_price, selected: true }

            //调用 store 中的 mutations ，来将商品加入购物车
            this.$store.commit('addToCar', goodsInfo)
        },
        beforeEnter(el){
            //transition 钩子函数
            el.style.transform = 'translate(0, 0)'
        },
        enter(el, done){
            //transition 钩子函数
            el.offsetWidth

            //获取小球在页面中的位置
            const ballPosition = this.$refs.ball.getBoundingClientRect()
            //获取购物车徽标 的位置
            const badgePosition = document.getElementById('badge').getBoundingClientRect()
            //计算相对的位置
            const xDist = badgePosition.left - ballPosition.left;
            const yDist = badgePosition.top - ballPosition.top;

            el.style.transform = `translate(${xDist}px, ${yDist}px)`
            el.style.transition = 'all .5s cubic-bezier(.4,-0.3,1,.68)'
            //表示完成之后立即执行结束
            done()
        },
        afterEnter(el){
            //transition 钩子函数
            this.ballFlag = !this.ballFlag
        }
    }
}
</script>

<style lang="sass" scoped>
.goods-info{
    background-color:#eee;
    overflow:hidden;

    .ball{
        position:absolute;
        top:365px;
        left:150px;
        z-index:999;
        width:20px;
        height:20px;
        border-radius:50%;
        background-color:red;
    }

    .mint-swipe{
        height:160px;

        .mint-swipe-item{
            img{
               width:100%;
               height:100%; 
            }
        }
    }
    .price{
        .old{
            b{
                text-decoration: line-through;
                margin-left:5px;
            }
        }
        .now{
             margin-left:10px;
            b{
                margin-left:5px;
                color:red;
                font-size:14px;
            }
        }
    }
    .opt{
        margin-top:10px;
    }

    .mui-card-footer, .mui-card-header{
        display:block;

        button{
            margin-top:10px;
        }
    }
}
</style>
